/**
 * Created at 2006-8-23, by Liao Xuefeng.
 * Copyright_2006, all rights reserved.
 */
package com.javaeedev.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.Pattern;

/**
 * User
 * 
 * @author xuefeng
 */
@Entity
@Table(name="T_USER")
public class User {

    public static final String REGEX_USERNAME = "[a-z0-9][a-z0-9\\-]{1,18}[a-z0-9]";
    public static final String REGEX_PASSWORD = "[a-f0-9]{32}";
    public static final String REGEX_EMAIL = "([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})";

    private String username;
    private String password;
    private boolean admin;
    private String email;
    private int emailValidation;
    private long createdDate;
    private long lockDate;

    public User() {}

    public User(String username, String password, boolean admin, long lastSignOnDate) {
        this.username = username;
        this.password = password;
        this.admin = admin;
    }

    @Id
    @Column(updatable=false, length=20)
    @Pattern(regex=REGEX_USERNAME)
    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }

    @Column(nullable=false, length=32)
    @Pattern(regex=REGEX_PASSWORD)
    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    @Column(nullable=false, length=50)
    @Pattern(regex=REGEX_EMAIL)
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    @Column(nullable=false)
    public boolean getAdmin() { return admin; }
    public void setAdmin(boolean admin) { this.admin = admin; }

    @Column(nullable=false, updatable=false)
    public long getCreatedDate() { return createdDate; }
    public void setCreatedDate(long createdDate) { this.createdDate = createdDate; }

    @Column(nullable=false)
    public int getEmailValidation() { return emailValidation; }
    public void setEmailValidation(int emailValidation) { this.emailValidation = emailValidation; }

    @Column(nullable=false)
    public long getLockDate() { return lockDate; }
    public void setLockDate(long lockDate) { this.lockDate = lockDate; }

    @Transient
    public boolean getEmailValidated() { return emailValidation==0; }

    @Transient
    public boolean getLocked() {
        return !admin && lockDate>0 && lockDate>System.currentTimeMillis();
    }
}
